package day14;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/remove-element/">27. 移除元素</a>
 * @author admin
 */
public class Answer {

    public static void main(String[] args) {
        Answer answer = new Answer();

        System.out.println(answer.removeElement(new int[]{0,4,4,0,4,4,4,0,2}, 4));;
    }

    public int removeElement(int[] nums, int val) {
        if (nums.length == 0) return 0;
        if (nums.length == 1 && val == nums[0]) return 0;
        int j = 0;
        int a = 0;
        int b = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == val) {
                j++;
                if (b + 1 != i) {
                    a = i;
                }
            } else {
                nums[a] = nums[i];
                a++;
            }
            b = i;
        }
        System.out.println(Arrays.toString(nums));
        return nums.length - j;
    }
}
